package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.TicketBusiness;
import com.ruoyi.system.domain.TicketCustomer;
import com.ruoyi.system.enums.BusType;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TicketSealMapper;
import com.ruoyi.system.domain.TicketSeal;

import javax.validation.constraints.Size;

/**
 * 印章信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-15
 */
@Service
public class TicketSealServiceImpl implements ITicketSealService
{
    @Autowired
    private TicketSealMapper ticketSealMapper;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private TicketCommonService commonService;

    @Autowired
    private ITicketBusinessService businessService;

    /**
     * 查询印章信息
     *
     * @param sealNo 印章信息主键
     * @return 印章信息
     */
    @Override
    public TicketSeal selectTicketSealBySealNo(String sealNo)
    {
        return ticketSealMapper.selectTicketSealBySealNo(sealNo);
    }

    /**
     * 查询印章信息列表
     *
     * @param ticketSeal 印章信息
     * @return 印章信息
     */
    @Override
    public List<TicketSeal> selectTicketSealList(TicketSeal ticketSeal)
    {
        List<TicketSeal> ticketSeals = ticketSealMapper.selectTicketSealList(ticketSeal);
        //判断当前登录人是否是商户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        SysUser sysUser = userService.selectUserById(userId);
        @Size(min = 0, max = 30, message = "用户昵称长度不能超过30个字符") String nickName = sysUser.getNickName();
        if(commonService.isBusiness()){
            TicketBusiness business = businessService.selectTicketBusinessByBusinessName(nickName);
            String businessPhone = business.getBusinessPhone();
            ticketSeals= ticketSeals
                    .stream()
                    .filter(p->p.getCreateBy().equals(businessPhone))
                    .collect(Collectors.toList());
            //判断是否是用户
        }
        return ticketSeals;

    }

    /**
     * 新增印章信息
     *
     * @param ticketSeal 印章信息
     * @return 结果
     */
    @Override
    public int insertTicketSeal(TicketSeal ticketSeal)
    {
        if(StringUtils.isEmpty(ticketSeal.getSealNo())){
            ticketSeal.setSealNo(UUID.randomUUID().toString());
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        ticketSeal.setCreateBy(loginUser.getUsername());
        ticketSeal.setCreateTime(DateUtils.getNowDate());
        return ticketSealMapper.insertTicketSeal(ticketSeal);
    }

    /**
     * 修改印章信息
     *
     * @param ticketSeal 印章信息
     * @return 结果
     */
    @Override
    public int updateTicketSeal(TicketSeal ticketSeal)
    {
        ticketSeal.setUpdateTime(DateUtils.getNowDate());
        return ticketSealMapper.updateTicketSeal(ticketSeal);
    }

    /**
     * 批量删除印章信息
     *
     * @param sealNos 需要删除的印章信息主键
     * @return 结果
     */
    @Override
    public int deleteTicketSealBySealNos(String[] sealNos)
    {
        return ticketSealMapper.deleteTicketSealBySealNos(sealNos);
    }

    /**
     * 删除印章信息信息
     *
     * @param sealNo 印章信息主键
     * @return 结果
     */
    @Override
    public int deleteTicketSealBySealNo(String sealNo)
    {
        return ticketSealMapper.deleteTicketSealBySealNo(sealNo);
    }
}
